﻿//创建时间: 2010-12-10
//创建作者: 李晓忠
//功能说明: CMS系统 评论模块 
//最后修改: 2010-12-10
using System;
using System.Collections.Generic;
using System.Text;


using TRPOP.CMS.Model;
using TRPOP.CMS.DALFactory;
using System.Data;
using System.Web.UI.WebControls;
using TRPOP.Config;
using TRPOP.Common;

namespace TRPOP.CMS.Business
{
    public class RecomManage
    {

        private IRecomDAL Idal;
        public RecomManage()
        {
            Idal = DataAccess.CreateRecomDAL();
        }

        #region 保存 Save
        ///	<summary>
        ///	保存
        ///	</summary>
        ///	<param name="RecomInfo"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult Save(RecomInfo info)
        {
            return Idal.Save(info);
        }

        #endregion

        #region 获取实体 GetInfoById
        ///	<summary>
        ///	获取实体
        ///	</summary>
        ///	<param name="recommendId"></param>
        ///	<returns>RecomInfo</returns>
        public RecomInfo GetInfoById(int id)
        {
            return Idal.GetInfoById(id);
        }
        #endregion

        #region 删除 DeleteInfoById
        ///	<summary>
        ///	删除
        ///	</summary>
        ///	<param name="id"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult DeleteInfoById(int id)
        {
            return Idal.DeleteInfoByIds(id.ToString());
        }
        #endregion

        #region 删除 DeleteInfoByIds
        ///	<summary>
        ///	删除
        ///	</summary>
        ///	<param name="ids"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult DeleteInfoByIds(string ids)
        {
            return Idal.DeleteInfoByIds(ids);
        }
        #endregion

        #region 自定义列表（无分页） GetListByCondition
        ///	<summary>
        ///	自定义列表（无分页）
        ///	</summary>
        ///	<param name="topn">前N条，为0时表示所有符合条件的</param>
        ///	<param name="condition">自定义条件，要以And开头</param>
        ///	<param name="orderby">排序，不需要Order by </param>
        ///	<returns>List<RecomInfo></returns>
        public List<RecomInfo> GetListByCondition(int topn, string condition, string orderby)
        {
            return Idal.GetListByCondition(topn, condition, orderby);
        }
        #endregion

        #region 自定义列表（分页） GetListByPager
        ///	<summary>
        ///	自定义列表（分页）
        ///	</summary>
        ///	<returns>List<RecomInfo></returns>
        public List<RecomInfo> GetListByPager(int pageNumber, int pageSize, string condition, string orderby, out int recordCount)
        {
            return Idal.GetListByPager(pageNumber, pageSize, condition, orderby, out  recordCount);
        }
        #endregion
         
        #region  评论
        /// <summary>
        /// 级联评论相关的表，获取相关数据
        /// </summary>
        /// <param name="NodeID"></param>
        /// <param name="intPageNum"></param>
        /// <param name="intPageSize"></param>
        /// <param name="intRecount"></param>
        /// <returns></returns>
        public DataTable GetRecommentDT(int NodeID, int intPageNum, int intPageSize, out int intRecount)
        {
            return Idal.GetRecommentDT(NodeID, intPageNum, intPageSize, out  intRecount);
        }

        /// <summary>
        /// 级联评论相关的表，获取相关数据
        /// </summary>
        /// <param name="NodeID"></param>
        /// <param name="intPageNum"></param>
        /// <param name="intPageSize"></param>
        /// <param name="intRecount"></param>
        /// <param name="intContentID"></param>
        /// <param name="timeStart"></param>
        /// <param name="timeEnd"></param>
        /// <returns></returns>
        public DataTable GetRecommentDT(int NodeID, int intPageNum, int intPageSize, out int intRecount,int intContentID,DateTime timeStart,DateTime timeEnd)
        {
            return Idal.GetRecommentDT(NodeID, intPageNum, intPageSize, out  intRecount,intContentID, timeStart, timeEnd);
        }
        /// <summary>
        /// 级联评论相关的表，获取相关数据
        /// </summary>
        /// <param name="NodeID"></param>
        /// <param name="intPageNum"></param>
        /// <param name="intPageSize"></param>
        /// <param name="intRecount"></param>
        /// <param name="intContentID"></param>
        /// <param name="timeStart"></param>
        /// <param name="timeEnd"></param>
        /// <param name="strSQLNodeIDsCondation">结点限制</param>
        /// <returns></returns>
        public DataTable GetRecommentDT(int NodeID, int intPageNum, int intPageSize, out int intRecount, int intContentID, DateTime timeStart, DateTime timeEnd, string strSQLNodeIDsCondation)
        {
            return Idal.GetRecommentDT(NodeID, intPageNum, intPageSize, out  intRecount, intContentID, timeStart, timeEnd, strSQLNodeIDsCondation);
        }

        /// <summary>
        /// 获取信息的评论数
        /// </summary>
        /// <param name="intIndexid"></param>
        /// <returns></returns>
        public int GetContentRecCount(int intIndexid)
        {
            return Idal.GetContentRecCount(intIndexid);
        }
        /// <summary>
        /// 获取结点的评论数
        /// </summary>
        /// <param name="intNodeid"></param>
        /// <returns></returns>
        public int GetNodeRecCount(int intNodeid)
        {
            return Idal.GetNodeRecCount(intNodeid);
        }

        /// <summary>
        /// 初始化模板设置 在结点设置页面里增添 评论模板设置
        /// </summary>
        /// <param name="ltTemplate"></param>
        /// <param name="intNodeid"></param>
        public static void NodeTemplateShow(Literal ltTemplate, int intNodeid)
        {
            string strTemplate = ltTemplate.Text;
            ltTemplate.Text = "";

            #region 评论插件的代码
            if (SYSConfig.RecommentConfigerInfo.Enable)
            {
                string strTemplateBaseRoot = string.Empty;
                string strTemplateShowRoot = string.Empty;
                RecomTemplateInfo oRecomTemplateInfo = new RecomTemplateInfo();
                oRecomTemplateInfo = new RecomTemplateManage().GetInfoById(intNodeid);
                if (oRecomTemplateInfo != null)
                {
                    string strDetailTemplate = oRecomTemplateInfo.TemplateRoot.Trim();
                    if (strDetailTemplate.StartsWith("/"))
                        strDetailTemplate = strDetailTemplate.Remove(0, 1);
                    if (strDetailTemplate.Contains("/"))
                    {
                        strTemplateBaseRoot = strDetailTemplate.Substring(0, strDetailTemplate.IndexOf("/"));
                        strTemplateShowRoot = strDetailTemplate.Substring(strDetailTemplate.IndexOf("/"));
                    }
                }
                ltTemplate.Text = string.Format(strTemplate, "评论模板", "Recomment")
                                        .Replace("$value1$", strTemplateShowRoot)
                                        .Replace("$value2$", strTemplateBaseRoot);

            }
            #endregion
        }
        /// <summary>
        /// 设置结点的评论模板
        /// </summary>
        /// <param name="intNodeid"></param>
        public static void NodeTemplateSave(int intNodeid)
        {
            if (SYSConfig.RecommentConfigerInfo.Enable)
            {
                string strRecomment = RequestHelper.RequestString("Recomment").Trim();
                string strRecommentRoot = RequestHelper.RequestString("RecommentRoot").Trim();
                if (strRecomment.IsEmpty() == false &&strRecommentRoot.IsEmpty() == false)
                {
                    RecomTemplateManage oRecomTemplateManage = new RecomTemplateManage();
                    RecomTemplateInfo oRecomTemplateInfo = oRecomTemplateManage.GetInfoById(intNodeid);
                    if (oRecomTemplateInfo == null)
                    {
                        oRecomTemplateInfo = new RecomTemplateInfo();
                        oRecomTemplateInfo.AddTime = DateTime.Now;
                    }
                    oRecomTemplateInfo.NodeId = intNodeid;
                    oRecomTemplateInfo.UpdateTime = DateTime.Now;
                    oRecomTemplateInfo.TemplateRoot = (strRecommentRoot.StartsWith("/") ? "" : "/") +
                                                      (strRecommentRoot.EndsWith("/") ? strRecommentRoot.Remove(strRecommentRoot.Length - 1) : strRecommentRoot) +
                                                      (strRecomment.StartsWith("/") ? strRecomment : ("/" + strRecomment));

                    oRecomTemplateManage.Save(oRecomTemplateInfo);
                }
            }
        }
        #endregion
    }
}


